import rospy
import cv2
from std_msgs.msg import Header
from sensor_msgs.msg import Image
from cv_bridge import CvBridge,CvBridgeError
import time
import numpy as np
cap=cv2.VideoCapture(2)
rospy.init_node("camera_node",anonymous=True)
image_pub=rospy.Publisher("/image_raw",Image,queue_size=1)
while not rospy.is_shutdown():
    start=time.time()
    ret,frame=cap.read()
    if ret:
        frame=cv2.flip(frame,1)
        ros_frame=Image()
        header=Header(stamp=rospy.Time.now())
        header.frame_id="camera"
        ros_frame.header=header
        ros_frame.width=640
        ros_frame.height=480
        ros_frame.encoding="bgr8"
        ros_frame.step=1920 
        ros_frame.data=np.array(frame).tobytes()
        image_pub.publish(ros_frame)
        end=time.time()
        rospy.loginfo("cost time:%d",end-start)
        rate=rospy.Rate(25)
cap.release()
cv2.destroyAllWindows()
print("successfully")